<?php
class item_randommoney extends template_item
{
	var $default_name = "Get Random money";
	var $default_description = "Get random money from {low} to {high}";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $item_config = array();
	var $description = "";
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item['item_name'] = str_replace(array('{low}','{high}'),array($this->item_config['lowest'],$this->item_config['highest']),$this->item['item_name']);
			
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['lowest'] = 0;
			$this->item_config['highest'] = 1000;
		}
		
		$this->description = str_replace(array('{low}','{high}'),array($this->item_config['lowest'],$this->item_config['highest']),$this->item['item_description']);
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{low} = The minimum amount of money to be gained<br />{high} = The maximum amount of money to be gained<br />Both are settable below.",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Minimum amount of money</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][item_lowest]", $this->item_config['lowest']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Maximum amount of money</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][item_highest]", $this->item_config['highest']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['lowest'] = $this->ipsclass->numbers->parse_input_number($input['item_lowest'],'int');
		$save['highest'] = $this->ipsclass->numbers->parse_input_number($input['item_highest'],'int');
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$amount = 0;
		foreach($inventory_ids as $inv_id)
		{
			$getting = $this->ipsclass->numbers->random_number($this->item_config['lowest'],$this->item_config['highest']);
			$amount = $this->ipsclass->numbers->add($amount, $getting);
			$this->ipsclass->market_class->record_reward($getting);
			$this->_write_log($inv_id, "Got ".$getting." from random money");
		}
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$amount)
				),"id=".$this->ipsclass->member['id']);
		
		$this->ipsclass->lang['gain_x_money'] = str_replace('<#AMOUNT#>', $amount, $this->ipsclass->lang['gain_x_money']);
		$this->ipsclass->lang['gain_x_money'] = str_replace('<#MONEY_NAME#>', $this->ipsclass->vars['market_currency'], $this->ipsclass->lang['gain_x_money']);
		
		$this->delete_items($inventory_ids);
		
		$this->ipsclass->market_class->redirect('gain_x_money');
	}
	
	function parse_store_description()
	{
		return $this->description;
	}
	
	function parse_group_description()
	{
		return $this->description;
	}
	
	function parse_single_description()
	{
		return $this->description;
	}
}
?>